#include "Sprite.h"
#include "DebugLog.h"

using namespace luabind;



Sprite::Sprite()
:
    _position(0.f, 0.f),
    _imageSize(0.f, 0.f),
    _clip(0.f, 0.f, 32.f, 32.f),
    _loaded(false)
{
    _textureMgr = TextureManager::instance();
}

Sprite::~Sprite()
{
    //dtor
}

/**
 if the texture is loaded it will bind it and draw the texture onto
 a quad using GL_QUADS
**/
void Sprite::render()
{
    if(_loaded)
    {
        glEnable(GL_TEXTURE_2D);
        sf::Texture::bind(&_texture);

        glPushMatrix();
            glTranslatef(_position.x, _position.y, 0);
            glBegin(GL_QUADS);
                glTexCoord2f(_clip.left/_imageSize.x, _clip.top/_imageSize.y); // maybe divide by _clip.width and _clip.height
                glColor4f(1.f,1.f,1.f,1.f);
                glVertex2f(-_clip.width/2.f, -_clip.height/2.f);

                glTexCoord2f((_clip.left + _clip.width)/_imageSize.x, _clip.top/_imageSize.y);
                glColor4f(1.f,1.f,1.f,1.f);
                glVertex2f(_clip.width/2.f, -_clip.height/2.f);

                glTexCoord2f((_clip.left + _clip.width)/_imageSize.x, (_clip.top + _clip.height)/_imageSize.y);
                glColor4f(1.f,1.f,1.f,1.f);
                glVertex2f(_clip.width/2.f, _clip.height/2.f);

                glTexCoord2f(_clip.left/_imageSize.x, (_clip.top + _clip.height)/_imageSize.y);
                glColor4f(1.f,1.f,1.f,1.f);
                glVertex2f(-_clip.width/2.f, _clip.height/2.f);
            glEnd();
        glPopMatrix();
    }
}


/// loads a texture from a file, sets the size to the size of the image
void Sprite::load(std::string filename)
{
    if(_texture.loadFromImage(_textureMgr->textureImage(filename)))
    {
        setLoaded(true);
        _clip.left = 0;
        _clip.top = 0;
        _clip.width = _texture.getSize().x;
        _clip.height = _texture.getSize().y;

        _imageSize.x = _texture.getSize().x;
        _imageSize.y = _texture.getSize().y;
    }
    else
    {
        setLoaded(false);
    }

    _fileName = filename;
}

/// loads the texture allowing you to set the position and the clip rect
void Sprite::load(std::string filename, float x, float y, float clipW, float clipH, float clipX, float clipY)
{
    if(_texture.loadFromImage(_textureMgr->textureImage(filename)))
    {
        setLoaded(true);
        _position = sf::Vector2f(x, y);
        _clip = sf::FloatRect(clipX, clipY, clipW, clipH);
        _imageSize.x = _texture.getSize().x;
        _imageSize.y = _texture.getSize().y;
    }
    else
    {
        setLoaded(false);
    }
    _fileName = filename;
}

/// moves by x width and by y height
void Sprite::move(float x, float y)
{
    _position.x += x;
    _position.y += y;
}


/// moves the sprite to the coords (x, y)
void Sprite::setPosition(float x, float y)
{
    _position.x = x;
    _position.y = y;
}
void Sprite::setPosition(sf::Vector2f position)
{
    _position = position;
}

/// sets the texture coords to (x, y, (x + w), (y + h))
void Sprite::setClip(float x, float y, float w, float h)
{
    _clip.left = x;
    _clip.top = y;
    _clip.width = w;
    _clip.height = h;
}
void Sprite::setClip(sf::FloatRect clip)
{
    _clip = clip;
}

/// if file dosent load it writes to the debug log
void Sprite::setLoaded(bool loaded)
{
    _loaded = loaded;
    if(!_loaded)
    {
        DebugLog::instance()->write("Image:");
        DebugLog::instance()->write(_fileName.c_str());
        DebugLog::instance()->writeLine("Not Loaded");
    }
}

/** Getter Functions *********************************************************/
float           Sprite::x()         { return _position.x;   }
float           Sprite::y()         { return _position.y;   }
float           Sprite::width()     { return _clip.width;       }
float           Sprite::height()    { return _clip.width;       }
float           Sprite::clipX()     { return _clip.left;       }
float           Sprite::clipY()     { return _clip.top;       }

sf::Vector2f    Sprite::position()  { return _position;     }
sf::FloatRect   Sprite::clip()      { return _clip;         }
